package his.controller;


import com.alibaba.fastjson.JSONObject;
import his.entity.Patient;
import his.service.PatientService;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    PatientService patientService;

    @PostMapping("/add")
    @ResponseBody
    public CommonResponse add_patient(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String token=(String) jsonObject.get("token");
        String realname=(String) jsonObject.get("name");
        String idnumber=(String) jsonObject.get("idnumber");
        int gender=Integer.parseInt((String) jsonObject.get("gender"));
        String birthdate=(String) jsonObject.get("birthdate");
        int age=Integer.parseInt((String) jsonObject.get("age"));
        String agetype=(String) jsonObject.get("agetype");
        String homeaddress=(String) jsonObject.get("homeaddress");

        return  patientService.add_patient(token,realname,gender,idnumber,birthdate,age,agetype,homeaddress);
    }

    @GetMapping("/getinfo")
    @ResponseBody
    public CommonResponse<Patient> get_patient_info(@RequestParam String token)
    {

        return  patientService.get_patient_info(token);
    }

    @PostMapping("/get_realname")
    @ResponseBody
    public CommonResponse<String> get_realname(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String casenumber=(String) jsonObject.get("CaseNumber");
        return  patientService.get_realname(casenumber);
    }


    @GetMapping("/token")
    @ResponseBody
    public CommonResponse<String> producce_token(@RequestParam String code)
    {
        System.out.println(code);
        return  patientService.producce_token(code);
    }
}
